package Server.Agent;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class CHttpClient {
	Log log = LogFactory.getLog( this .getClass());   
	public static String getMD5(String str) {
	    try {
	        // 生成一个MD5加密计算摘要
	        MessageDigest md = MessageDigest.getInstance("MD5");
	        // 计算md5函数
	        md.update(str.getBytes());
	        // digest()最后确定返回md5 hash值，返回值为8为字符串。因为md5 hash值是16位的hex值，实际上就是8位的字符
	        // BigInteger函数则将8位的字符串转换成16位hex值，用字符串来表示；得到字符串形式的hash值
	        return new BigInteger(1, md.digest()).toString(16);
	    } catch (Exception e) {
	    	e.printStackTrace();
	    }
	    return null;
	}
	public String addmoneyinfo(int userid, int money, String orderno) {

		long time = System.currentTimeMillis() / 1000;
		String miyao = "mj123456789";
		//String host = "dlzjh.cjpoker.com";
		String host = GameConfig.getInstance().get("APP_HOST");
		if(host == null)
		{
			return "";
		}		
		String token = String.format("%d%d%s", userid, time, miyao);
		String tokenmd5 = getMD5(token);
		String urlchar = String.format("http://%s/dlIndex.php?m=Index&c=togame&a=addmoneyinfo&money=%d&userid=%d&token=%s&orderno=%s&time=%d",host, money,userid, tokenmd5,orderno,time);
		System.out.println("userid==" + userid);
		System.out.println("money==" + money);
		System.out.println("orderno==" +orderno);
		System.out.println("urlchar==" +urlchar);
		return Get(urlchar);
	}
	private String Get(String urlchar) {

		HttpClient client = new HttpClient();
		client.setTimeout(4000);
		HttpMethod method = new GetMethod(urlchar);
		try {
			client.executeMethod(method);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}

		// 打印服务器返回的状态
		//System.out.println(method.getStatusLine());
		// 打印返回的信息
		try {
			return method.getResponseBodyAsString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		}
		// 释放连接
		method.releaseConnection();
		return null;
	}
	private String GetHttp(String urlchar) {

		HttpClient client = new HttpClient();
		client.setTimeout(1000);
		HttpMethod method = new GetMethod(urlchar);
		try {
			client.executeMethod(method);
		} catch (IOException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}

		// 打印服务器返回的状态
		//System.out.println(method.getStatusLine());
		// 打印返回的信息
		try {
			return method.getResponseBodyAsString();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error(e.getMessage());
		}
		// 释放连接
		method.releaseConnection();
		return null;
	}

	public String reguserinfo(int userid, int code, String nickname, String phone, String wxnum) {

		long time = System.currentTimeMillis() / 1000;
		
		String miyao = "mj123456789";
		//String host = "dlzjh.cjpoker.com";
		String host = GameConfig.getInstance().get("APP_HOST");
		if(host == null)
		{
			return "";
		}
		String token = String.format("%d%d%s", userid, time, miyao);
		String tokenmd5 = getMD5(token);
		String encode_nickname = "";
		try {
			encode_nickname = URLEncoder.encode(nickname,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		String urlchar = String.format("http://%s/merchants.php?m=Index&c=togame&a=reguserinfo&userid=%d&code=%d&nickname=%s&phone=%s&wxnum=%s&token=%s&time=%d",
				host, userid, code, encode_nickname, phone, wxnum, tokenmd5, time);
		return Get(urlchar);
	}
	public void registerGateInfo(String nodeInfo){
		try{
			String host = GameConfig.getInstance().get("APP_HOST");
			if(host == null)
			{
				return ;
			}
			String urlchar = String.format("http://%s/app/server.php?op=addgate&ip=%s",
					host, nodeInfo);
			Get(urlchar);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void unregisterGateInfo(String nodeInfo){
		try{
			String host = GameConfig.getInstance().get("APP_HOST");
			if(host == null)
			{
				return ;
			}
			String urlchar = String.format("http://%s/app/server.php?op=removegate&ip=%s",
					host, nodeInfo);
			Get(urlchar);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void refreshGateInfo(String nodeInfo){
		try{
			String host = GameConfig.getInstance().get("APP_HOST");
			if(host == null)
			{
				return ;
			}
			String urlchar = String.format("http://%s/app/server.php?op=refreshgate&ip=%s",
					host, nodeInfo);
			Get(urlchar);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public void dwcautoTx(String url){
		try{
			Get(url);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public String requestHttpInfo(String url){
		try{
			return GetHttp(url);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
